const { getCartGoods, cartChecked, deleteCartGoods, updateCartGoods} = require('../../api/index.js')
Component({
  data: {
    cartObj: {},
    editStatus: false,
    _cartObj: {},
    _checkedArr: [],
    _checkedAmount: 0
  },
  methods: {
    async getCartGoods() { //获取购物车商品
      const res = await getCartGoods()
      console.log(res)
      this.setData({
        cartObj: res
      })
    },
    async cartChecked(e) { //购物车选中未选中
      const {index} = e.currentTarget.dataset
      const res = await cartChecked({
        isChecked: this.data.cartObj.cartList[index].checked === 1 ? 0 : 1,
        productIds: this.data.cartObj.cartList[index].product_id
      })
      this.setData({
        cartObj: res
      })
    },
    _cartChecked(e) { //编辑状态下购物车选中未选中
      const { index } = e.currentTarget.dataset
      let str = `_cartObj.cartList[${index}].checked`
      this.setData({
        [str]: this.data._cartObj.cartList[index].checked ? 0 : 1
      })
      let arr = this.data._cartObj.cartList
      let _arr = [], count = 0
      arr.map(item => {
        if(item.checked) {
          _arr.push(item)
          count += item.number
        }
      })
      this.setData({
        _checkedArr: _arr,
        _checkedAmount: count
      })
    },
    async cartCheckedAll(e) { //全选
      const { id } = e.currentTarget
      let str = ''
      this.data.cartObj.cartList.map(item => {
        str += item.product_id + ','
      })
      const res = await cartChecked({
        isChecked: id,
        productIds: str
      })
      this.setData({
        cartObj: res
      })
    },
    _cartCheckedAll(e) { //编辑状态下全选 非全选
      let {id} = e.currentTarget
      let arr = this.data._cartObj.cartList
      let _arr = [],count = 0,checkedArr = []
      arr.map(item => {
        if(id==1) {
          item.checked = 1
          _arr.push(item)
          count += item.number
          checkedArr.push(item)
        }else{
          item.checked = 0
          _arr.push(item)
          count = 0
          checkedArr = []
        }
      })
      this.setData({
        '_cartObj.cartList': _arr,
        '_checkedArr': checkedArr,
        '_checkedAmount': count
      })
    },
    edit() { //点击编辑按钮
      let obj = JSON.parse(JSON.stringify(this.data.cartObj))
      let _arr = []
      obj.cartList.map(item => {
        item.checked = 0
        _arr.push(item)
      })
      this.setData({
        editStatus: true,
        '_cartObj.cartList': _arr,
        '_cartObj.cartTotal.goodsCount': obj.cartTotal.goodsCount,
        '_cartObj.cartTotal.goodsAmount': obj.cartTotal.goodsAmount,
        '_cartObj.cartTotal.checkedGoodsCount': 0,
        '_cartObj.cartTotal.checkedGoodsAmount': 0,
        _checkedArr: [],
        _checkedAmount: 0
      })
    },
    finish() { //点击完成
      this.setData({
        editStatus: false,
        _checkedArr: [],
        _checkedAmount: 0
      })
      this.getCartGoods()
    },
    async deleteCartGoods() { //删除商品
      if (!this.data._cartObj.cartList || this.data._cartObj.cartList.length<=0) return;
      let str = ""
      let arr = this.data._cartObj.cartList
      arr.map(item => {
        if(item.checked == 1) {
          str += (item.product_id + ',')
        }
      })
      if(str != "") {
        const res = await deleteCartGoods(str) 
        this.setData({
          cartObj: res
        })
        this.edit()
      }
    },
    async updateCartGoods(e) { //购物车数据加减
      let {num,index} = e.currentTarget.dataset;
      let list = this.data._cartObj.cartList;
      const res = await updateCartGoods({ 
        "productId": list[index].product_id, 
        "goodsId": list[index].goods_id, 
        "number": list[index].number + num, 
        "id": list[index].id 
      })
      let str = `_cartObj.cartList[${index}].number`
      this.setData({
        [str]: res.cartList[index].number
      })
      let arr = this.data._cartObj.cartList
      let _arr = [], count = 0
      arr.map(item => {
        if (item.checked) {
          _arr.push(item)
          count += item.number
        }
      })
      this.setData({
        _checkedArr: _arr,
        _checkedAmount: count
      })
    }
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        })
      }
      this.getCartGoods()
    }
  }
})
